import { _decorator, AudioSource, Label, labelAssembler, Node, tween, Tween, v3 } from 'cc';
import { BaseLayer } from './BaseLayer';
import { AudioMgr } from '../manager/AudioMgr';
import { ENUM_AUDIO_CLIP, ENUM_UI_LAYER } from '../Enum';
import { Ins } from '../Ins';
import { DataMgr } from '../manager/DataMgr';
import { formatTimer } from '../Util';
import { SdkMgr } from '../manager/SdkMgr';
import { EffMgr } from '../manager/EffMgr';
const { ccclass, property } = _decorator;

@ccclass('MainLayer')
export class MainLayer extends BaseLayer {
    btn_exit: Node = null
    count_down: Node = null
    count_down_sound: AudioSource = null
    ui_notice: Node = null
    skills: Node = null
    skill_tip: Node = null

    onLoad() {
        this.btn_exit = this.node.getChildByName('btn_exit')
        this.btn_exit.on('click', this.onExitClick, this)
        this.count_down = this.node.getChildByName('count_down')
        const sound = this.node.getChildByName('count_down_sound')
        this.count_down_sound = sound.getComponent(AudioSource)
        this.ui_notice = this.node.getChildByName('ui_notice')
        this.skills = this.node.getChildByName('skills')
        this.skill_tip = this.node.getChildByName('skill_tip')
        for (let i = 0; i < this.skills.children.length; i++) {
            const skill = this.skills.children[i]
            if (!skill.hasEventListener('click')) {
                skill.on('click', () => {
                    this.onSkillClick(i)
                }, this)
            }
        }
    }

    onDestroy() {
        this.btn_exit.off('click', this.onExitClick, this)
    }

    onExitClick() {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        Ins.UIMgr.toggle(ENUM_UI_LAYER.EXIT)
    }

    reset() {
        // 倒计时
        this.stopCountDown()
        // 警告提示
        Tween.stopAllByTarget(this.ui_notice)
        if (DataMgr.ins.isHard) this.startNotice()
        this.toggleSkillTip(false)
        if (DataMgr.ins.isResetSkillBtn) {
            // 重置技能按钮
            this.skills.children.forEach((skill: Node, i: number) => {
                skill.getChildByName('icon_ad').active = i !== 0
            })
        }
    }

    startNotice() {
        const { y } = this.ui_notice.getPosition()
        this.ui_notice.setPosition(600, y)
        tween().target(this.ui_notice)
            .to(0.3, { position: v3(0, y) })
            .delay(0.5)
            .to(0.3, { position: v3(-600, y) })
            .start()
    }

    startCountDown() {
        const label = this.count_down.getComponent(Label)
        label.string = formatTimer(DataMgr.ins.countDown, 'i:s')
        label.schedule(() => {
            if (DataMgr.ins.countDown <= 1) this.stopCountDown()
            DataMgr.ins.countDown -= 1
            if (DataMgr.ins.countDown < 0) DataMgr.ins.countDown = 0
            label.string = formatTimer(DataMgr.ins.countDown, 'i:s')
            if (DataMgr.ins.countDown <= DataMgr.ins.countDownSound) {
                this.setCountDownSound(true)
            }
            if (DataMgr.ins.countDown <= 0) {
                this.setCountDownSound(false)
                Ins.GameMgr.onGameOver(ENUM_UI_LAYER.LOSE)
            }
        }, 1)
    }

    stopCountDown() {
        const label = this.count_down.getComponent(Label)
        label.unscheduleAllCallbacks()
        this.setCountDownSound(false)
    }

    setCountDownSound(bool: boolean) {
        if (bool) {
            if (!this.count_down_sound.playing) {
                this.count_down_sound.play()
            }
        } else {
            this.count_down_sound.stop()
        }
    }

    onSkillClick(index: number) {
        AudioMgr.ins.playSound(ENUM_AUDIO_CLIP.CLICK)
        const skill = this.skills.children[index]
        if (skill.getChildByName('icon_ad').active) {
            this.stopCountDown()
            SdkMgr.ins.showVideoAd(() => {
                this.startCountDown()
                Ins.GameMgr.onGameSkill(index)
            }, (str: string) => {
                this.startCountDown()
                EffMgr.ins.showTip('EffTip', str)
            })
        } else {
            skill.getChildByName('icon_ad').active = true
            Ins.GameMgr.onGameSkill(index)
        }
    }

    toggleSkillTip(bool: boolean) {
        this.skill_tip.active = bool
    }

    getSkillTip() {
        return this.skill_tip.active
    }
}


